
var WebModel = {
    m_bIsConnected: false,
    m_strCode: "021BpU5T06HnKY1b745T0rMj6T0BpU53",

    load: function ()
    {
        cc.log("WebModel on Load");

        if (cc.sys.platform === cc.sys.WECHAT_GAME)
        {
              wx.onShow(function callback(res) {
                console.log('返回前台');
                if (WebModel.m_bIsConnected == false && cc.find("Canvas"))
                {
                    cc.find("Canvas").emit('event_game_off_line');
                }
              });

              wx.onHide(function callback(res) {
                console.log('前往后台 ', wx.getPerformance().now());
              });

              wx.showShareMenu();

              wx.login({
  				success(res) {
                    WebModel.m_strCode = res.code;
  					console.log('login code :', res.code);
  				}
  			  });

              // const button = wx.createUserInfoButton({
              //             type: 'text',
              //             text: '开始游戏',
              //             style: {
              //               left: 10,
              //               top: 76,
              //               width: 200,
              //               height: 40,
              //               lineHeight: 40,
              //               backgroundColor: '#ff0000',
              //               color: '#ffffff',
              //               textAlign: 'center',
              //               fontSize: 16,
              //               borderRadius: 4
              //             }
              //           });

              // button.onTap((res) => {
              //     console.log(res)
              // });

              // wx.getSetting({
              //     success(res) {
              //       if (res.authSetting["scope.userInfo"] == false)
              //       {
              //           const button = wx.createUserInfoButton({
              //             type: 'text',
              //             text: '开始游戏',
              //             style: {
              //               left: 10,
              //               top: 76,
              //               width: 200,
              //               height: 40,
              //               lineHeight: 40,
              //               backgroundColor: '#ff0000',
              //               color: '#ffffff',
              //               textAlign: 'center',
              //               fontSize: 16,
              //               borderRadius: 4
              //             }
              //           });

              //         button.onTap((res) => {
              //             console.log(res)
              //         })
              //       }
              //       else
              //       {
              //       }
              //   }
              // });
        }
    },

    connectToServer: function ()
    {
        var Take6Model = require("Take6Model");

        // webSocket
        // ws://104.153.103.147:3369 : server
        // ws://192.168.1.33:3369 : home
        // ws://172.16.1.105:3369 : company
        // wss://www.5kmtian.xyz:443/wss
        // 
        this.ws = new WebSocket("ws://172.16.1.232:3369");
        cc.find("Canvas").emit('event_sending_message_to_server');

        this.m_bIsFirstConnect = false;
        this.ws.onopen = function (event) {
            console.log("WebSocket was opened.");
            WebModel.m_bIsConnected = true;

            cc.find("Canvas").emit('event_web_opened');

            if (cc.sys.platform === cc.sys.WECHAT_GAME)
            {
              WebModel.getUserDataFromServer();
            }
        };
        this.ws.onmessage = function (event) {
            console.log("WebSocket onmessage:", event.data);
            WebModel.parseServerMessage(event.data);

            cc.find("Canvas").emit('event_web_onmessage');
        };
        this.ws.onerror = function (event) {
            console.log("Send Text fired an error");
            WebModel.m_bIsConnected = false;

            cc.find("Canvas").emit('event_web_onerror');
        };
        this.ws.onclose = function (event) {
            if (cc.sys.platform === cc.sys.WECHAT_GAME)
            {
                console.log("WebSocket instance closed. ", wx.getPerformance().now());
            }

            WebModel.m_bIsConnected = false;
            cc.find("Canvas").emit('event_web_onclose');
        };
    },

    sendMessageToServer : function (message, withOutMask)
    {
        cc.log("message to server :", message);
        this.ws.send(message);

        if (withOutMask != true)
        {
        	cc.find("Canvas").emit('event_sending_message_to_server');
        }
    },

    parseServerMessage : function (message)
    {
        var Take6Model = require("Take6Model");

        var json = JSON.parse(message);
        var messageType = json.messageType;
        switch(messageType)
        {
            case "user_data":
                Take6Model.paseUserDataMessageJson(json);
                break;
            case "room_create":
                Take6Model.paseCreateRoomMessageJson(json);
                break;
            case "room_join":
                Take6Model.paseJoinRoomMessageJson(json);
                break;
            case "room_exit":
                Take6Model.paseExitRoomMessageJson(json);
                break;
            case "game_start":
                Take6Model.parseGameStartMessageJson(json);
                break;
            case "game_play":
                Take6Model.parseGamePlayMessageJson(json);
                break;
            case "game_continue":
                Take6Model.parseGameContinueMessageJson(json);
                break;
            case "error_no_room":
                cc.find("Canvas").emit('event_error_no_room',
                {
                });
                break;
        }
    },

    getUserDataFromServer : function ()
    {
        var json = 
        {
            "messageType" : "user_data",
            "userCode" : WebModel.m_strCode
        };

        var jsonStr = JSON.stringify(json);
        WebModel.sendMessageToServer(jsonStr, true);
    },

    testFunction : function ()
    {
        cc.log("testFunction");
        console.log('WebModel function');

        var Take6Model = require("Take6Model");
        Take6Model.getACard();
    },
};
WebModel.load();

module.exports = WebModel;
